// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database();

// 云函数入口函数
// 添加一条数据到home_list表
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let {name,intro,type,imgs,foods} = event;

  imgs = imgs.map(el=>{
    return {
      url:el._url
    }
  }) || []
  let obj = {
    name:name,
    type:type,
    imgs:imgs,
    openid:wxContext.OPENID
  };

  let homeRes = await db.collection("home_list").add({
    data:obj
  });

  let classify = {
    name:name,
    intro:intro,
    foods:foods,
    type:type,
    openid:wxContext.OPENID
  };

  let classifyRes = await db.collection("classify").add({
    data:classify
  })

  return {
    code:200,
    msg:"发布成功"
  }
}